const router = require('koa-router')()

const { loginCheck, loginRedirect } = require('../middlewares/loginChecks')

router.get('/', loginRedirect, async (ctx, next) => {
  // throw "今天的天气不错呢"
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/error', async (ctx, next) => {
  await ctx.render('error')
})

router.get('/string', loginCheck ,async (ctx, next) => {
  
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  // 在这里测试session 把
  // 通过ctx 获取session 
  let session = ctx.session
  // 有就用原来的 ， 没有就是0
  session.count = session.count || 0
  session.count++

  ctx.body = {
    title: 'session测试',
    count: session.count
  }
})

module.exports = router
